﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityClient;
using System.Configuration;

namespace DeepTalkDBSerice
{
    /// <summary>
    /// Manages the connections to a database. These utilities
    /// are used by everyone to access the database.
    /// </summary>
    class DBUtilities
    {
        /// <summary>
        /// The connection string to use to load the database.
        /// </summary>
        private static string connectionString = null;

        /// <summary>
        /// Get a connection to the database
        /// </summary>
        /// <returns></returns>
        public static ConferenceModelContainer GetConferenceContainer()
        {
            AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);
            if (connectionString == null)
            {
                return new ConferenceModelContainer();
            }
            else
            {
                /// Using something other than the connection string in our app.config file.
                EntityConnectionStringBuilder ec = new EntityConnectionStringBuilder(ConfigurationManager.ConnectionStrings["ConferenceModelContainer"].ConnectionString);
                ec.ProviderConnectionString = connectionString;
                return new ConferenceModelContainer(ec.ToString());
            }
        }

        /// <summary>
        /// Remember the connection string to use instead of the default connection.
        /// </summary>
        /// <param name="p"></param>
        internal static void SetDBConnectionString(string conn)
        {
            connectionString = conn;
        }
    }
}
